package com.tpp.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpp.comm.pojo.*;
import com.tpp.comm.util.OrderLineUpException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpp.order.feign.CinemaFeign;
import com.tpp.order.feign.ItemFeign;
import com.tpp.order.feign.TaskFeign;
import com.tpp.order.mapper.OrderMapper;
import com.tpp.order.feign.LinkUserFeign;
import com.tpp.order.mapper.OrderLinkUserMapper;
import com.tpp.order.service.OrderService;
import com.tpp.order.vo.RabbitOrderMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderLinkUserMapper orderLinkUserMapper;

    @Resource
    private CinemaFeign cinemaFeign;

    @Resource
    private ItemFeign itemFeign;

    @Resource
    private LinkUserFeign linkUserFeign;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private TaskFeign taskFeign;

    @Transactional
    @Override
    public void createOrder(TpTask task) {
        RabbitOrderMessage orderMessage = null;
        try {
            orderMessage = objectMapper.readValue(task.getRequestBody(), RabbitOrderMessage.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        //远程调用，查询商品
        String[] seats = orderMessage.getSeatPositions().split(",");
        TpItem tpItem = itemFeign.findById(orderMessage.getItemId());

        //添加订单
        TpOrder tpOrder = new TpOrder();
        BeanUtils.copyProperties(orderMessage,tpOrder);
        tpOrder.setItemName(tpItem.getItemName());
        tpOrder.setId(null);
        tpOrder.setTotalCount(seats.length);
        tpOrder.setTotalAmount(orderMessage.getAmount());
        tpOrder.setCreatedTime(LocalDateTime.now());
        tpOrder.setUpdatedTime(null);

        orderMapper.insert(tpOrder);

        String[] linkIds = orderMessage.getLinkIds().split(",");
        String[] prices = orderMessage.getPrice().split("_");

        for (int i = 0; i < seats.length; i++) {
            String[] seat = seats[i].split("_");
            TpLinkUser linkUser = linkUserFeign.findById(Long.parseLong(linkIds[i]));
            TpOrderLinkUser tpOrderLinkUser = new TpOrderLinkUser();
            tpOrderLinkUser.setOrderId(tpOrder.getId());
            tpOrderLinkUser.setLinkUserId(linkUser.getId());
            tpOrderLinkUser.setLinkUserName(linkUser.getName());
            tpOrderLinkUser.setX(Integer.parseInt(seat[0]));
            tpOrderLinkUser.setY(Integer.parseInt(seat[1]));
            tpOrderLinkUser.setPrice(Double.parseDouble(prices[i]));
            tpOrderLinkUser.setCreatedTime(LocalDateTime.now());
            orderLinkUserMapper.insert(tpOrderLinkUser);
        }
    }

    @Override
    public TpOrder searchOrder(String orderNo) {
        //查询订单是否创建
        TpTask byOrderNo = taskFeign.findByOrderNo(orderNo);
        if(byOrderNo==null) throw new RuntimeException("非法参数,请退出收银台后支付");
        LambdaQueryWrapper<TpOrder> orderWrapper = Wrappers.lambdaQuery();
        orderWrapper.eq(TpOrder::getOrderNo,orderNo);
        TpOrder tpOrder = orderMapper.selectOne(orderWrapper);
        if(tpOrder==null) throw new OrderLineUpException("订单排队中，请稍后...");
        return tpOrder;
    }

    @Override
    public void updateOrderStatus(String orderNo,Integer orderType) {
        orderMapper.update(null,new LambdaUpdateWrapper<TpOrder>()
                .set(TpOrder::getOrderType,orderType)
                .eq(TpOrder::getOrderNo,orderNo));
    }
}
